package com.project.service;

import com.project.common.utils.MyPageHelper;
import com.project.common.utils.Util;
import com.project.dao.UserMapper;
import com.project.entity.User;
import com.project.myBeans.ConfigParameters;
import com.project.service.base.BaseService;
import com.project.service.base.PasswordHelper;
import com.project.service.base.cryptolib.CryptoApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = {Exception.class})
@Service
public class UserServiceImpl extends BaseService implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordHelper passwordHelper;

    @Autowired
    private ConfigParameters configParameters;


    @Override
    public void add(User user) throws Exception{

        if(user.getPassword()==null){
            user.setPassword(configParameters.getDefaultPassword());
        }
        passwordHelper.encryptPassword(user); // 加密密码
        userMapper.insertSelective(user);
    }

    @Override
    public void remove(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatch(List idList) {
        userMapper.deleteBatch(idList);
    }

    @Override
    public int modify(User user) {
        User oldUser = findAllById(user.getId());
        //加密密码,需作判断，密码未改变则无需加密
        if (user.getPassword() != null && !user.getPassword().isEmpty() && !oldUser.getPassword().equals(user.getPassword()))//与原密码不相同，则加密
            passwordHelper.encryptPassword(user);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        filterSensitiveInfo(user);
        return user;
    }

    @Override
    public User findAllById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User findByUserName(String userName) {
        return userMapper.selectByUsername(userName);
    }

    @Override
    public List<User> findBatchByCondition(User user) {

        MyPageHelper.setPageParam(user);

        return userMapper.selectBatchByCondition(user);
    }

    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 验证密码是否正确
     *
     * @param sessionUser 当前user
     * @param password    被验证密码
     * @return
     */
    @Override
    public Boolean validatePassword(User sessionUser, String password) {
        try {
            String oldPassword = Util.bytesToHexString(CryptoApp.PwdTransValue(password.getBytes(), Util.hexStringToBytes(sessionUser.getSalt())));
            return oldPassword.equals(sessionUser.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User findByWorkId(String workid) {
        return userMapper.selectByWorkId(workid);
    }
}
